module etc.dleveldb.options;

import deimos.leveldb.c;

import etc.dleveldb.env;
import etc.dleveldb.cache;
import etc.dleveldb.filterpolicy;
import etc.dleveldb.snapshot;


import std.exception;


class Comparator
{
    package:
    @property
    inout(leveldb_comparator_t*) ptr() inout
    {
        return this._comparator;
    }
    private:
    leveldb_comparator_t* _comparator;
}


class Options
{
    this()
    {
        enforce(_options = leveldb_options_create(), "Failed to create options");

    }
    ~this()
    {
        if(this.isValid){
            leveldb_options_destroy(this._options);
        }
        this._options = null;
    }

    @property:
    void create_if_missing(bool val) => leveldb_options_set_create_if_missing(this._options, val);
    void error_if_missing(bool val) => leveldb_options_set_error_if_exists(this._options, val);
    void paranoid_checks(bool val) => leveldb_options_set_paranoid_checks(this._options, val);
    void compression(int val) => leveldb_options_set_compression(this._options, val);
    void write_buffer_size(size_t val) => leveldb_options_set_write_buffer_size(this._options, val);
    void max_open_files(int val) => leveldb_options_set_max_open_files(this._options, val);
    void block_size(size_t val) => leveldb_options_set_block_size(this._options, val);
    void block_restart_interval(int val) => leveldb_options_set_block_restart_interval(this._options, val);
    void env(Environment env) => leveldb_options_set_env(this._options, (env !is null)?env.ptr:null);
    void cache(Cache cache) => leveldb_options_set_cache(this._options, (cache !is null)?cache.ptr:null);
    void filter_policy(FilterPolicy policy) => leveldb_options_set_filter_policy(this._options, (policy !is null)?policy.ptr:null);
    void comparator(Comparator comparator) => leveldb_options_set_comparator(this._options, (comparator !is null)?comparator.ptr:null);


    bool isValid() inout
    {
        return (this._options !is null);
    }


    package:
    @property
    inout(leveldb_options_t*) ptr() inout
    {
        return _options;
    }
    private:
    leveldb_options_t*   _options;
}



class ReadOptions
{
    this()
    {
        enforce(_options = leveldb_readoptions_create(), "Failed to create read options");
    }
    ~this()
    {
        if(this.isValid){
            leveldb_readoptions_destroy(this._options);
            this._options = null;
        }
    }

    @property:
    void verify_checksums(bool val) => leveldb_readoptions_set_verify_checksums(this._options, val);
    void fill_cache(bool val) => leveldb_readoptions_set_fill_cache(this._options, val);
    void snapshot(Snapshot snapshot) => leveldb_readoptions_set_snapshot(this._options, snapshot.ptr);

    bool isValid() inout
    {
        return (this._options !is null);
    }

    package:
    @property
    inout(leveldb_readoptions_t*) ptr() inout
    {
        return _options;
    }
    private:
    leveldb_readoptions_t*   _options;
}

class WriteOptions
{
    this()
    {
        enforce(_options = leveldb_writeoptions_create(), "Failed to create write options");
    }
    ~this()
    {
        if(this.isValid){
            leveldb_writeoptions_destroy(this._options);
            this._options = null;
        }
    }

    @property:
    void sync(bool val) => leveldb_writeoptions_set_sync(this._options, val);

    bool isValid() inout
    {
        return (this._options !is null);
    }

    package:
    @property
    inout(leveldb_writeoptions_t*) ptr() inout
    {
        return _options;
    }
    private:
    leveldb_writeoptions_t*   _options;
}


@property
public __gshared const(ReadOptions) DefaultReadOptions()
{
    static ReadOptions _defaultReadOptions;
    if(_defaultReadOptions is null){
        _defaultReadOptions = new ReadOptions();
    }
    return _defaultReadOptions;
}


@property
public __gshared const(WriteOptions) DefaultWriteOptions()
{
    static WriteOptions _defaultWriteOptions;
    if(_defaultWriteOptions is null){
        _defaultWriteOptions = new WriteOptions();
    }
    return _defaultWriteOptions;
}